﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace Easydp.Models
{
    public class TradeManager
    {
        public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 

        public int Update(Entities.TT_Trade model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TT_Trade set ");
            strSql.Append("OrderID=@OrderID,");
            strSql.Append("IsPaid=@IsPaid,");
            strSql.Append("PayAmount=@PayAmount,");
            strSql.Append("TradeNo=@TradeNo,");
            strSql.Append("OrderNumber=@OrderNumber,");
            strSql.Append("BuyerID=@BuyerID,");
            strSql.Append("BuyerEmail=@BuyerEmail,");
            strSql.Append("SellerID=@SellerID,");
            strSql.Append("SellerEmail=@SellerEmail,");
            strSql.Append("CreateOn=@CreateOn");
            strSql.Append(" where TradeID=@TradeID ");
           
         
            using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@OrderID", model.OrderID);
                updation.AddParameter("@IsPaid", model.IsPaid);
                updation.AddParameter("@PayAmount", model.PayAmount);
                updation.AddParameter("@TradeNo", model.TradeNo);
                updation.AddParameter("@OrderNumber", model.OrderNumber);
                updation.AddParameter("@BuyerID", model.BuyerID);
                updation.AddParameter("@BuyerEmail", model.BuyerEmail);
                updation.AddParameter("@SellerID", model.SellerID);
                updation.AddParameter("@SellerEmail", model.SellerEmail);
                updation.AddParameter("@CreateOn", model.CreateOn);
             
                return updation.ExecuteNonQuery();

            }

        }

        public int Insert(Entities.TT_Trade obj)
        {
            using (SqlStoredProcedure insertion = new SqlStoredProcedure("Pro_TT_Trade_Pan_Insert"))
            {
                insertion.AddParameter("@OrderID", obj.OrderID);
                insertion.AddParameter("@IsPaid", obj.IsPaid);
                insertion.AddParameter("@PayAmount", obj.PayAmount);
                insertion.AddParameter("@TradeNo", obj.TradeNo);
                insertion.AddParameter("@OrderNumber", obj.OrderNumber);
                insertion.AddParameter("@BuyerID", obj.BuyerID);
                insertion.AddParameter("@BuyerEmail", obj.BuyerEmail);
                insertion.AddParameter("@SellerID", obj.SellerID);
                insertion.AddParameter("@SellerEmail", obj.SellerEmail);
                insertion.AddParameter("@PayType", obj.PayType);
                insertion.AddParameter("@Dec", obj.Dec);
                insertion.AddParameter("@NotifyID", obj.NotifyID);
                insertion.AddParameter("@NotifyTime", obj.NotifyTime);


                return insertion.ExecuteNonQuery();
            }
        }

        #region Get Mothed
        /// <summary>
        /// 得到model一个对象实体
        /// </summary>
        /// 
        public Entities.TT_Trade GetModel(int orderid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 OrderID,IsPaid,PayAmount,TradeNo,OrderNumber,BuyerID,BuyerEmail,SellerID,SellerEmail,PayType,Dec,CreateOn,NotifyID,NotifyTime from TT_Trade ");
            strSql.Append(" where OrderID=@OrderID and IsPaid = 1 ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderID", SqlDbType.Int)};
            parameters[0].Value = orderid;

            Entities.TT_Trade model = new Entities.TT_Trade();
            using (SqlText selection = new SqlText(strSql.ToString()))
            {
                selection.AddParameter("@OrderID", orderid);

                while (selection.Read())
                {
                    if (selection["OrderID"].ToString() != "")
                    {
                        model.OrderID = int.Parse(selection["OrderID"].ToString());
                    }
                    model.IsPaid = Convert.ToBoolean(selection["IsPaid"]);
                    model.PayAmount = decimal.Parse(selection["PayAmount"].ToString());
                    model.TradeNo = selection["TradeNo"].ToString();
                    model.OrderNumber = selection["OrderNumber"].ToString();
                    model.BuyerID = selection["BuyerID"].ToString();
                    model.BuyerEmail = selection["BuyerEmail"].ToString();
                    model.PayType = selection["PayType"].ToString();
                    model.Dec = selection["Dec"].ToString();
                    model.SellerID = selection["SellerID"].ToString();
                    model.SellerEmail = selection["SellerEmail"].ToString();
                    model.NotifyTime = DateTime.Parse(selection["NotifyTime"].ToString());
                    model.CreateOn = DateTime.Parse(selection["CreateOn"].ToString());
                }
            }
            model.PushInfo = (new PushFeeManager()).GetModel(model.OrderID);
            return model;





            
           
        }



        

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDadaset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM TT_Trade ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            using (SqlConnection sqlcon = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    sqlcon.Open();
                    SqlDataAdapter command = new SqlDataAdapter(strSql.ToString(), connectionString);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    sqlcon.Close();
                }
                return ds;
            }

        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetFunds(string strSql)
        {
            return DbHelperSQL.Query(strSql);
        }


        /// <summary>
        /// 得到list列表
        /// </summary>
        /// 
        public List<Entities.TT_Trade> GetList(int TradeID)
        {
            List<Entities.TT_Trade> list = new List<Easydp.Models.Entities.TT_Trade>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  TradeID,OrderID,IsPaid,PayAmount,TradeNo,OrderNumber,BuyerID,BuyerEmail,SellerID,SellerEmail,PayType,Dec,CreateOn from TT_Trade ");
            strSql.Append(" where TradeID=@TradeID ");
            using (SqlText selectlist = new SqlText(strSql.ToString()))
            {
                selectlist.AddParameter("@TradeID", TradeID);
                while (selectlist.Read())
                {
                    Entities.TT_Trade model = new Easydp.Models.Entities.TT_Trade();
                    if (selectlist["TradeID"].ToString() != "")
                    {
                        model.TradeID = int.Parse(selectlist["TradeID"].ToString());
                    }
                    if (selectlist["OrderID"].ToString() != "")
                    {
                        model.OrderID = int.Parse(selectlist["OrderID"].ToString());
                    }
                    if (selectlist["IsPaid"].ToString() != "")
                    {
                        if ((selectlist["IsPaid"].ToString() == "1") || (selectlist["IsPaid"].ToString().ToLower() == "true"))
                        {
                            model.IsPaid = true;
                        }
                        else
                        {
                            model.IsPaid = false;
                        }
                    }
                    if (selectlist["PayAmount"].ToString() != "")
                    {
                        model.PayAmount = decimal.Parse(selectlist["PayAmount"].ToString());
                    }
                    model.TradeNo = selectlist["TradeNo"].ToString();
                    model.OrderNumber = selectlist["OrderNumber"].ToString();
                    model.BuyerID = selectlist["BuyerID"].ToString();
                    model.BuyerEmail = selectlist["BuyerEmail"].ToString();
                    model.SellerID = selectlist["SellerID"].ToString();
                    model.PayType = selectlist["PayType"].ToString();
                    model.Dec = selectlist["Dec"].ToString();
                    model.SellerEmail = selectlist["SellerEmail"].ToString();
                    if (selectlist["CreateOn"].ToString() != "")
                    {
                        model.CreateOn = DateTime.Parse(selectlist["CreateOn"].ToString());
                    }
                    list.Add(model);
                }
                
            }
            return list;
        }

        #endregion

    }
}
